﻿import { combineReducers } from 'redux';
import * as types from '@/store/action-types'
import * as utils from '@/utils/index.js'
import config from '@/config'
import _const from '@/const'

function _handleAddNode(state, payload, newTree) {
	console.log("ADD_NODE",payload)
	// var newTree= _.cloneDeep(payload.data.nodeTree)
	// var newTree= _.cloneDeep(state)
	var node=utils.getNodeByPath(newTree, payload.data.path)
	let new_node =_.get(payload.data,'node',{})
	let newNode = _.merge({}, {
		children:[],
		attr:{
		},
		selector: [],
		positionBy: [],
		styles: {},
		tag: payload.data.tag
	},new_node)
	if(newNode.tag=='text'){
		newNode.text="I'm text"
	}
	console.log("newNode ****", newNode)
	utils.addUnit(newNode)
	if(node){
		node.children.push(newNode)
	}else{
		newTree.push(newNode)
	}
}

var timestamp = new Date().getTime()

function nodeTree(state = [], payload) {
	switch (payload.type) {
		case types.ADD_NODE:
			var newTree = _.cloneDeep(state)
			if(((new Date().getTime()) - timestamp) > 100) {
				_handleAddNode(state, payload, newTree)
				timestamp = new Date().getTime()
			}
			return newTree;
		case types.DEL_NODE:
			var newTree= _.cloneDeep(state)
			var path = _.clone(payload.data.path)
			// 获得父级节点的path
			var index = path.pop()
			var node=utils.getNodeByPath(newTree, path)
			// 删除节点
			// _.drop(node.children, index)
			node.children.splice(index,1)
			return newTree
		case types.NODE_UPDATE_BY_PATH:
			var newTree = _.cloneDeep(state)
			var node=utils.getNodeByPath(newTree,payload.data.path)
			if(node.isRoot){
				_.merge(node, payload.data.node)
				return newTree
			} //拦截对根节点的操作
			
			let {act='', actPath=''} = payload.data
			
			if(payload.data.node){
				let _node = payload.data.node
				utils.addUnit(_node)
				
				if(_.get(_node.attr,['class','0']) && _node.selector.length==0){
					_node.selector=[`.${_node.attr.class[0]}`]
				}
			}
			
			_.merge(node, payload.data.node)

			if(act){
				_[act](node,actPath)
			}

			return newTree
		default:
			return state
	}
}
function currentNode(state = [], action) {
	switch (action.type) {
		case types.CURRENT_NODE_CHANGE:
			var node=utils.getNodeByPath(action.data.nodeTree,action.data.node.path)
			Object.assign(node,action.data.node)
			return node;
		default:
			return state
	}
}
/* function currentHandle(state = [], action){
	switch (action.type) {
		case types.UPDATE_CURRENT_HANDLE:
			var newData= Object.assign({},state,action.data);
			return newData
		default:
			return state
	}
} */
function operationMode(state ="", action){
	switch (action.type) {
		case types.OPERATION_MODE_CHANGE:
			return action.data
		default:
			return state
	}
}
function currentPath(state ="", action){
	switch (action.type) {
		case types.UPDATE_CURRENT_PATH:
			return action.data
		default:
			return state
	}
}
function currentDom(state ="", action){
	switch (action.type) {
		case types.UPDATE_CURRENT_DOM:
			return action.data
		default:
			return state
	}
}
function inputStatus(state ="", action){
	switch (action.type) {
		case types.UPDATE_INPUT_STATUS:
			return action.data
		default:
			return state
	}
}
function tag(state ="", action){
	switch (action.type) {
		case types.UPDATE_TAG:
			return action.data
		default:
			return state
	}
}
function sizeFix(state ="", action){
	switch (action.type) {
		case types.UPDATE_SIZE_FIX:
			return action.data
		default:
			return state
	}
}
function exportStatus(state = false, action){
	switch (action.type) {
		case types.UPDATE_EXPORT_STATUS:
			return action.data
		default:
			return state
	}
}
function license(state={}, action){
	switch (action.type) {
		case types.UPDATE_LICENSE:
			let _license = _.cloneDeep(state)
			_.merge(_license, action.data)
			return action.data
		default:
			return state
	}
}
function showLicenseDialog(state = false, action){
	switch (action.type) {
		case types.UPDATE_LICENSE_DIALOG_S:
			return action.data
		default:
			return state
	}
}
function slideNumber(state = false, action){
	switch (action.type) {
		case types.UPDATE_SLIDE_NUMBER:
			return Object.assign({},state, action.data)
		default:
			return state
	}
}
function editConfig(state=false, action){
	switch (action.type) {
		case types.SET_CONFIG_STATUS:
			return action.data
		default:
			return state
	}
}

const reducer = combineReducers({
	license,
	currentDom,
	currentPath,
	nodeTree,
	currentNode,
	operationMode,
	inputStatus,
	tag,
	sizeFix,
	exportStatus,
	showLicenseDialog,
	slideNumber,
	editConfig
});

export default reducer
